package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;

public class DownloadGear360ViewerDialog extends DialogFragment {

    private String mMessage;

    public static DownloadGear360ViewerDialog getInstance(String message) {
        DownloadGear360ViewerDialog dialog = new DownloadGear360ViewerDialog();
        dialog.mMessage = message;
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        String titleText = getResources().getString(R.string.unable_to_view_file);
        builder.setTitle(titleText);
        builder.setMessage(mMessage);
        builder.setPositiveButton(R.string.download,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                        goToDownloadPage();
                    }
                });
        builder.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });
        return builder.create();
    }

    private void goToDownloadPage() {
        Activity activity = getActivity();
        if (activity != null) {
            try {
                final Intent intent = new Intent();
                intent.setData(Uri.parse("samsungapps://ProductDetail/com.samsung.android.gear360viewer"));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
